(function() {
  app.factory('storage', ['$localStorage', '$sessionStorage', 'tradeApi', function($localStorage, $sessionStorage, tradeApi) {
    var storage = {};

    storage.local = function(key, value) {
      return value ? $localStorage[key] = value : $localStorage[key];
    };

    storage.session = function(key, value) {
      return value ? $sessionStorage[key] = value : $sessionStorage[key];
    };

    return storage;
  }])

  .service('returnData', function($location, $timeout, $filter) {
    var that = this;
    this.dateSet = function(type, split) {
      var start = new Date('September 01, 2017 00:00:00').valueOf();
      var now_date = (new Date()).valueOf();

      var day7 = now_date - 6 * 1000 * 24 * 60 * 60;
      var day15 = now_date - 14 * 1000 * 24 * 60 * 60;
      var day90 = now_date - 89 * 1000 * 24 * 60 * 60;
      var split_ = split ? split : 'yyyy-MM-dd';

      if (type === 1) {
        return {
          startdate: $filter('date')(now_date, split_),
          enddate: $filter('date')(now_date, split_)
        };
      }
      if (type === 7) {
        return {
          startdate: $filter('date')(day7, split_),
          enddate: $filter('date')(now_date, split_)
        };
      }
      if (type === 15) {
        return {
          startdate: $filter('date')(day15, split_),
          enddate: $filter('date')(now_date, split_)
        };
      }
      if (type === 16) {
        return {
          enddate: $filter('date')(day15, split_),
        };
      }
      if (type === 90) {
        return {
          startdate: $filter('date')(day90, split_),
          enddate: $filter('date')(now_date, split_)
        };
      }
      if (type === '20170901') {
        return {
          startdate: $filter('date')(start, split_),
          enddate: $filter('date')(now_date, split_)
        };
      }
    };
  })

  .factory('local_m', function() {
    var set = function(key, data, type) {
      var exist_obj = angular.fromJson(localStorage.getItem('local_m')) || { tab: {}, data: {} };
      exist_obj[type ? type : 'data'][key] = data;
      localStorage.setItem('local_m', angular.toJson(exist_obj));
    };
    var get = function(key, type) {
      var exist_obj = angular.fromJson(localStorage.getItem('local_m'));
      return exist_obj[type ? type : 'data'][key];
    };
    return {
      set: set,
      get: get
    };
  });
})();